<?php
namespace dk\core;

class Loader{
    protected static $instance;
    /**
    * @return Loader
    */
    public static function &getInstance() {
        if (!isset(static::$instance)) {
            self::$instance = new static();
        }

        return static::$instance;
    }
    
    /**
    * put your comment there...
    * 
    * @var \Composer\Autoload\ClassLoader
    */
    private $loader = null;
    /**
    * put your comment there...
    * 
    * @return \Composer\Autoload\ClassLoader
    */
    public function getLoader(){
        if(!$this->loader){
            $this->loader = include(dirname(dirname(__DIR__)) . '/vendor/autoload.php');
        }
        return $this->loader;
    }
    
    public function myAutoLoad($config = []){
        spl_autoload_register(array($this, 'loadClass'),true, true);
        if(!$config){
            return false;
        }
        $this->initMVC($config);
    }
    
    public function loadClass($class){
        if($file = $this->myFindFile($class)){
            include $file;
            return true;
        }
    }
    
    private $models = [];
    private $mvc = [];
    protected function myFindFile($class){
        if(!$this->mvc){
            return false;
        }
  
        if ('\\' == $class[0]) {
            $class = substr($class, 1);
        }
        $class = explode('\\', $class);
        $type = array_shift($class);
        if(isset($this->mvc[$type])){
            $paths = $this->mvc[$type];
            foreach ($paths as $file_path) {
                $file = $file_path. implode('/', $class) . '.php';
                if(file_exists($file)){
                    return $file;
                }
            }
            
            $file = '';
            $tmp = $class;
            $siteName = strtolower(array_shift($tmp));
            switch ($type) {
                case 'action':
                    if(!$tmp){
                        return false;
                    }
                    $file = PATH_COM . 'module/' . $siteName . '/app/' . APP_NAME . '/action/' . $siteName . '/' . implode('/', $tmp) . '.php';
                    break;
                case 'model':
                    $file = PATH_COM . 'module/' . $siteName . '/model/' . $siteName . '/' . implode('/', $tmp) . '.php';
                    break;
                case 'api':
                    $file = PATH_COM . 'module/' . $siteName . '/api/' . $siteName . '/' . implode('/', $tmp) . '.php';
                    break;
                case 'widget':
                    if(!$tmp){
                        $tmp = $class;
                    }
                    $file = PATH_COM . 'module/' . $siteName . '/widget/' . implode('/', $tmp) . '.php';
                    break;
                case 'view':
                    
                    break;
            }
            if($file && file_exists($file)){
                return $file;
            }
        }
    }
    
    public function addMVC($name, $path){
        $this->mvc[$name][] = $path;
    }

    public function initMVC($config){
        $this->mvc = $config;
    }
    
    
    private $widgetDir = [];
    public function loadWidgetDir($tpl, $args = []){
        if(!$this->widgetDir){
            $this->widgetDir = [
                PATH_APP . 'widget/tpl/',
                PATH_COM . 'common/widget/tpl/',
            ];
        }

        $file = $tpl . (isset($args['tpl']) ? '_' . $args['tpl'] : '') . '.php';
        $widgetDir = $this->widgetDir;
        foreach ($widgetDir as $dir) {
            if(file_exists($dir . $file)){
                return $dir;
            }
        }
        $tmp = explode('/', $tpl);
        $siteName = array_shift($tmp);
        $dir = PATH_COM . 'module/' . $siteName . '/widget/tpl/';
        if(file_exists($dir . $file)){
            return $dir;
        }
    }
    
}